﻿using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    /// <summary>
    /// 按钮
    /// </summary>
    public class Button_listServices : BaseServices<button_list, string>, IButton_listServices
    {
        private readonly IButton_listRepository button_ListRepository;
        private readonly IAuthority_listServices authority_ListServices;
        private readonly IAuthority_list_inforServices authority_List_InforServices;
        private readonly IPermission_button_tableRepository permission_Button_TableRepository;

        public Button_listServices(IButton_listRepository button_ListRepository, IAuthority_listServices authority_ListServices, IAuthority_list_inforServices authority_List_InforServices, IPermission_button_tableRepository permission_Button_TableRepository) : base(button_ListRepository)
        {
            this.button_ListRepository = button_ListRepository;
            this.authority_ListServices = authority_ListServices;
            this.authority_List_InforServices = authority_List_InforServices;
            this.permission_Button_TableRepository = permission_Button_TableRepository;
        }

        /// <summary>
        /// RBAC的显示
        /// </summary>
        /// <returns>RBAC的显示</returns>
        //public List<Authority_listInfor_One_DTO> GetButtonLinforShow()
        //{
        //    //先获取所有的一级权限
        //    var olist = authority_ListServices.GetShowInfor();
        //    //获取所有的二级权限
        //    var tlist = authority_List_InforServices.GetShowInfor();
        //    //获取所有的按钮
        //    var blist = button_ListRepository.GetShowInfor();
        //    //获取所有的权限按钮
        //    List<permission_button_table>? tblist = permission_Button_TableRepository.GetShowInfor();
        //
        //    var mlist = (from a in olist
        //                 select new Authority_listInfor_One_DTO
        //                 {
        //                     authority_numbe = a.authority_numbe,
        //                     authority_name = a.authority_name,
        //                     authority_ListInfor_Two_DTOs = (from b in tlist
        //                                                     where (a.authority_numbe == b.fid)
        //                                                     select new Authority_listInfor_Two_DTO
        //                                                     {
        //                                                         aid = b.aid,
        //                                                         aname = b.aname,
        //                                                         fid = b.fid,
        //                                                         authority_Button_Three_DTOs = (from c in blist
        //                                                                                        join d in tblist
        //                                                                                        on c.button_number equals d.button_number
        //                                                                                        where (b.aid == d.authority_number)
        //                                                                                        select new Authority_button_Three_DTO
        //                                                                                        {
        //                                                                                            button_number = c.button_number,
        //                                                                                            button_name = c.button_name,
        //                                                                                        }).ToList(),
        //                                                     }).ToList()
        //                 }).ToList();
        //    foreach (var item in mlist)
        //    {
        //        foreach (var twoer in item.authority_ListInfor_Two_DTOs)
        //        {
        //            twoer._ListInfor_Two_DTOs = tlist.Where(c => c.fid == twoer.aid).Select(c => new Authority_listInfor_Two_DTO
        //            {
        //                aid = c.aid,
        //                aname = c.aname,
        //                _ListInfor_Two_DTOs = GetShow(blist, tblist, tlist,c.fid)
        //            }).ToList();
        //        }
        //    }
        //
        //    return mlist;
        //}
        //public List<Authority_listInfor_Two_DTO> GetShow(List<button_list> blist,List<permission_button_table> tblist, List<authority_list_infor> mlist,string? mid)
        //{
        //    var list = mlist.Where(a => a.fid == mid).Select(c => new Authority_listInfor_Two_DTO
        //    {
        //        aid = c.aid,
        //        aname = c.aname,
        //        authority_Button_Three_DTOs = (from d in blist
        //                                       join e in tblist
        //                                       on d.button_number equals e.button_number
        //                                       where (c.aid == e.authority_number)
        //                                       select new Authority_button_Three_DTO
        //                                       {
        //                                           button_number = e.button_number,
        //                                           button_name = d.button_name,
        //                                       }).ToList(),
        //        _ListInfor_Two_DTOs = GetShow(blist, tblist, mlist, c.aid)
        //    }).ToList();
        //    return list.Count()==0?null : list;
        //}


        /// <summary>
        /// AI生成 值得参考
        /// </summary>
        /// <returns></returns>
        public List<Authority_listInfor_One_DTO> GetButtonLinforShow()
        {
            // 获取数据
            var olist = authority_ListServices.GetShowInfor();
            var tlist = authority_List_InforServices.GetShowInfor();
            var blist = button_ListRepository.GetShowInfor();
            var tblist = permission_Button_TableRepository.GetShowInfor();

            // 定义递归函数，用于获取子列表信息
            Func<string?, List<Authority_listInfor_Two_DTO>?> getSubList = null;
            getSubList = (mid) =>
            {
                var list = tlist.Where(a => a.fid == mid).Select(c => new Authority_listInfor_Two_DTO
                {
                    aid = c.aid,
                    aname = c.aname,
                    authority_Button_Three_DTOs = (from d in blist
                                                   join e in tblist
                                                   on d.button_number equals e.button_number
                                                   where c.aid == e.authority_number
                                                   select new Authority_button_Three_DTO
                                                   {
                                                       button_number = c.aid + "_" + e.button_number,
                                                       button_name = d.button_name,
                                                   }).ToList(),
                    _ListInfor_Two_DTOs = getSubList(c.aid)
                }).ToList();

                return list.Count == 0 ? null : list;
            };

            // 构造返回结果
            var mlist = (from a in olist
                         select new Authority_listInfor_One_DTO
                         {
                             authority_numbe = a.authority_numbe,
                             authority_name = a.authority_name,
                             authority_ListInfor_Two_DTOs = (from b in tlist
                                                             where a.authority_numbe == b.fid
                                                             select new Authority_listInfor_Two_DTO
                                                             {
                                                                 aid = b.aid,
                                                                 aname = b.aname,
                                                                 fid = b.fid,
                                                                 authority_Button_Three_DTOs = (from c in blist
                                                                                                join d in tblist
                                                                                                on c.button_number equals d.button_number
                                                                                                where b.aid == d.authority_number
                                                                                                select new Authority_button_Three_DTO
                                                                                                {
                                                                                                    button_number = b.aid + "_" +  c.button_number,
                                                                                                    button_name = c.button_name,
                                                                                                }).ToList(),
                                                                 _ListInfor_Two_DTOs = getSubList(b.aid)
                                                             }).ToList()
                         }).ToList();

            return mlist;
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
    }
}
